package org.test.client.ui.component.combox;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;

import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIUtil;

/**
 * Combox
 * 
 * @author Guodeqi
 *
 */
//最小宽度 80
public class MyCombox extends JToggleButton implements FocusListener{
	public static final int MIN_WIDTH = 80;
	
	private MyCombox thisBox = this;
	
	public static Color borderColor = Icons.getColor("aaaaaa");
	public static Color highLightBorderColor = Icons.getColor("5897fb");
	public static Color back1 = Color.white;
	public static Color back2 = Icons.getColor("f8f8f8");
	public static Color back3 = Icons.getColor("f0f0f0");
	public static Color back4 = Icons.getColor("f2f2f2");
	
	public static Color fontColor = Color.black;
	
	public static int diameter = 16;
	
	public Color iconColor = Icons.getColor("888888");
	public String dropDownIcon = "fa-caret-down";
	public String dropUpIcon = "fa-caret-up";
	public BufferedImage biDown = null;
	public BufferedImage biUp = null;
	
	int w = 0;
	int h = 0;
	
	int prew = 0;
	int preh = 0;
	
	
	public ComboxContentContainer ccc = null;
	public MyComboxModel model = null;
	public boolean needSearch = true;
	public int iconw = 14;
	
	public String noSelect = "No item selected...";
	
	public MyCombox(MyComboxModel model){
		this.model = model;
		model.combox = this;
		doInit();
	}
	
	public MyCombox(MyComboxModel model,String placeHolder){
		this.model = model;
		this.noSelect = placeHolder;
		model.combox = this;
		doInit();
	}
	
	public void doInit(){
		biDown = Icons.getImgByDesc(dropDownIcon, iconw, iconColor);
		biUp = Icons.getImgByDesc(dropUpIcon, iconw, iconColor);
		
		this.setOpaque(false);
	
		addActionListener();
		addComponentListener();
		FontMetrics fm = this.getFontMetrics(this.getFont());
		preh = UIUtil.getTxtHeight(fm)+diameter;
		prew = UIUtil.getStringWidth(noSelect, fm) + diameter + iconw + 2;
		this.setPreferredSize(new Dimension(prew,preh));
		
		this.addFocusListener(thisBox);
	}
	
	
	public void addActionListener(){
		this.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionevent) {
				if(thisBox.isSelected()){
					Point point = new Point(0,0);
					SwingUtilities.convertPointToScreen(point , thisBox);
					if(ccc == null){
						JFrame frame = null;
						Container p = thisBox.getParent();
						for(int i =0;i<50;i++){
							if(p instanceof JFrame){
								frame = (JFrame) p;
								break;
							}else{
								p = p.getParent();
							}
						}
						ccc = new ComboxContentContainer(thisBox, frame);
					}
					int sx = point.x;
					int sy =  point.y+ h;
					int sw = w;
					int sh = ccc.preHeight;
					ccc.showList(sx, sy, sw, sh);
				}else{
					ccc.setVisible(false);
				}
			}
		});
	}
	
	/**
	 * 增加监听
	 * 若果 这个组件变化 则 对应 下拉列表也要变化
	 * 若frame 变化 下拉列表也要变化
	 */
	public void addComponentListener(){
		this.addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent e) {
				updateDropDownList();
			}
			public void componentMoved(ComponentEvent e) {
				updateDropDownList();
			}
		});
	}
	
	public void updateDropDownList(){
		
		if(ccc!= null && ccc.isVisible()){
			Point point = new Point(0,0);
			SwingUtilities.convertPointToScreen(point , thisBox);
			int sx = point.x;
			int sy =  point.y+ h;
			int sw = w;
			int sh = ccc.preHeight;
			ccc.setLocation(sx, sy);
			ccc.setSize(sw,sh);
		}
	}
	
	@Override
	protected void paintComponent(Graphics g) {
//		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D)g;
		UIUtil.setAntiAliasing(g2d, true);
		w = this.getWidth();
		h = this.getHeight();
		
		//paintBackGround
		if(isSelected()){
			Paint paint = UIUtil.getPaint(back4, back1, 0, 0, 0, h);
			g2d.setPaint(paint);
			g2d.fillRoundRect(0, 0, w, h, diameter, diameter);
		}else{
			Paint paint1 = UIUtil.getPaint(back1, back2, 0, 0, 0, h/2);
			Paint paint2 = UIUtil.getPaint(back3, back4, 0, h/2, 0, h);
			g2d.setPaint(paint1);
			g2d.fillRoundRect(0, 0, w, h/2, diameter, diameter);
			g2d.setPaint(paint2);
			g2d.fillRoundRect(0, h/2, w, h, diameter, diameter);
		}
		
		// paintborder
		Color tmp = borderColor;
		if(isFocusOwner()){
			tmp = highLightBorderColor;
		}
		int tmpw = w -1;
		int tmph = h -1;
		BufferedImage bi = biDown;
		if(isSelected()){
			tmph = h+diameter/2;
			tmp = borderColor;
			bi = biUp;
		}
		g2d.setColor(tmp);
		g2d.drawRoundRect(0, 0, tmpw, tmph, diameter, diameter);
		
//		paintTxt
		String txt = noSelect;
		Object selectedObj = model.getSelectedObj();
		if(selectedObj != null){
			txt = selectedObj.toString();
		}
		int x = diameter/2;
		int y = h/2 + UIUtil.getTxty(g2d);
		g2d.setColor(fontColor);
		g2d.drawString(txt, x, y);
		
		x = w - bi.getWidth() - diameter /2 ;
		y = h/2 - bi.getHeight()/2;
		
		g2d.drawImage(bi, x, y, null);
	}
	
	//屏蔽原来的边框
	@Override
	protected void paintBorder(Graphics g) {
//		super.paintBorder(g);
	}
	
	public void setSelectedObj(Object selectedObj) {
		model.selectedObj = selectedObj;
		ccc.setVisible(false);
		this.setSelected(false);
	}
	
	public Object getSelectedObj() {
		return model.selectedObj;
	}
	
	
	@Override
	public void setPreferredSize(Dimension dimension) {
		if(dimension.width < MIN_WIDTH){
			dimension.width = MIN_WIDTH;
		}
		super.setPreferredSize(dimension);
	}
	
	@Override
	public void setBounds(int i, int j, int k, int l) {
		if(k < MIN_WIDTH){
			k = MIN_WIDTH;
		}
		super.setBounds(i, j, k, l);
	}
	
	@Override
	public void setSize(int i, int j) {
		if(i < MIN_WIDTH){
			i = MIN_WIDTH;
		}
		super.setSize(i, j);
	}
	
	//清空所有下拉项
	public void clear(){
		if(ccc!= null){
			ccc = null;
		}
		model.clear();
		this.repaint();
	}

	@Override
	public void focusGained(FocusEvent arg0) {
		
	}

	
	/**
	 * 增加focus监听
	 * 实现不需要搜索的下拉列表自动隐藏
	 */
	@Override
	public void focusLost(FocusEvent arg0) {
		if(ccc!=null && ccc.isVisible()){
			if(!needSearch){
				ccc.setVisible(false);
				this.setSelected(false);
			}else{
//				if(!ccc.searchField.isFocusOwner()){
//					ccc.setVisible(false);
//				}
			}
		}
	}
}
